/**
 * Created by LiYang on 16-9-27 下午2:07.
 *
 */

package com.example.db.mapper;

import android.database.Cursor;


import com.example.db.utils.JdbcUtil;

import java.sql.SQLException;


public class SingleMapperHandler<T> extends RowMapperHandler<T> {
    private String TAG = "singleMapperHandler";

    private Class<T> requiredType;

    public SingleMapperHandler() {
    }

    public SingleMapperHandler(Class<T> superClass) {
        this.requiredType = superClass;
    }

    @SuppressWarnings("unchecked")
    public T rowMapper(Cursor cursor) throws SQLException {
        Object val = JdbcUtil.getColumnValue(cursor, 0);
        if (requiredType != null && !requiredType.isAssignableFrom(val.getClass())) {
            throw new ClassCastException(
                    "must be of " + requiredType + ", but was actually of " + val.getClass());
        }
        return (T) val;
    }
}
